package com.open.capacity.notice.vo;

import java.util.Objects;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.open.capacity.notice.dmo.AbstractSendEntity;
import com.open.capacity.notice.dmo.SendMessageJob;
import com.open.capacity.notice.enums.RececiverType;
import com.open.capacity.notice.enums.SendMessageJobStatus;

import cn.hutool.core.codec.Base64;
import io.swagger.annotations.ApiModelProperty;
import io.vavr.control.Try;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * <p>
 * 发送任务
 * </p>
 * 
 * @since 2024-05-06
 */
@Data
@NoArgsConstructor
public class SendMessageJobVO extends AbstractSendEntity {

	private static final long serialVersionUID = 1088269216824416034L;

	@ApiModelProperty(value = "任务名称")
	private String jobName;

	@ApiModelProperty(value = "关联消息模板Id")
	private String templateId;

	@ApiModelProperty(value = "关联消息模板名称")
	private String templateName;

	@ApiModelProperty(value = "接收类型:消息的发送ID类型：1.接收组 2.userId 3.手机号 4.微信")
	private RececiverType receciverType;

	@JsonIgnore
	@ApiModelProperty(value = "消息解析内容")
	private String sendMessage;

	@ApiModelProperty(value = "接收组Id")
	private String receciver;

	@ApiModelProperty(value = "接收组名称")
	private String receciverName;

	@ApiModelProperty(value = "触发类型")
	private Boolean timer;

	@ApiModelProperty(value = "cron表达式")
	private String cron;

	@ApiModelProperty(value = "屏蔽")
	private Boolean shield;

	@ApiModelProperty(value = "cron表达式")
	private String shieldTime;

	@ApiModelProperty(value = "模板变量")
	private String variables;

	@ApiModelProperty(value = "个性配置变量")
	private String extendInfo;

	private SendMessageJobStatus status;

	public SendMessageJobVO(SendMessageJob sendMessageJob) {
		if (Objects.nonNull(sendMessageJob)) {
			setId(sendMessageJob.getId());
			setJobName(sendMessageJob.getJobName());
			setTemplateId(sendMessageJob.getTemplateId());
			setRececiverType(sendMessageJob.getRececiverType());
			setRececiver(sendMessageJob.getRececiver());
			setSendMessage(sendMessageJob.getSendMessage());
			setTimer(sendMessageJob.getTimer());
			setCron(sendMessageJob.getCron());
			setShield(sendMessageJob.getShield());
			setShieldTime(sendMessageJob.getShieldTime());
			setVariables(
					Try.of(() -> new String(Base64.decode(sendMessageJob.getVariables().getBytes()))).getOrElse("{}"));
			setExtendInfo(Try.of(() -> JSON.toJSONString(sendMessageJob.getExtendInfo())).getOrElse("{}"));
			setCreateUser(sendMessageJob.getCreateUser());
			setCreateUserId(sendMessageJob.getCreateUserId());
			setCreateTime(sendMessageJob.getCreateTime());
			setUpdateUser(sendMessageJob.getUpdateUser());
			setUpdateUserId(sendMessageJob.getUpdateUserId());
			setUpdateTime(sendMessageJob.getUpdateTime());
			setDelFlag(sendMessageJob.getDelFlag());
		}

	}

}
